package Schedule::Chronic::Distributed::TaskHistory;

=head1 NAME

Schedule::Chronic::Distributed::TaskHistory - Task History

=head1 SYNOPSIS

use Schedule::Chronic::Distributed::TaskHistory;

Schedule::Chronic::Distributed::TaskHistory->create_from_task($task);

=head1 DESCRIPTION

Task History class, represents the history of a task, providing times, status, warnings and 
errors of each attempt to run a task.

=head1 METHODS

=head2 ACCESSORS

=over

=item id

=item task_name 

=item task 

=item command

=item command_arguments

=item status 

=item node 

=item priority 

=item error 

=item warning 

=item started 

=item finished 

=item due 

=item deadline

=back

=head2 create_from_task

Creates a new task history object from the provided task object, called when task fails, succedes or times out.

Schedule::Chronic::Distributed::TaskHistory->create_from_task($task);

=cut

use strict;

use Time::Piece;
use base qw(Schedule::Chronic::Distributed::DBI);

__PACKAGE__->table('task_history');
__PACKAGE__->sequence('task_history_id_seq');
__PACKAGE__->columns(All => qw/id task_name task command command_arguments status node priority error warning started finished due deadline/);
__PACKAGE__->columns(Primary => 'id');

__PACKAGE__->has_a (task => 'Schedule::Chronic::Distributed::Task');
__PACKAGE__->has_a (node => 'Schedule::Chronic::Distributed::Node');

__PACKAGE__->has_a( deadline => 'Time::Piece',
		    inflate => sub { return __PACKAGE__->inflate_timepiece(@_,'deadline'); },
		    deflate => sub { return __PACKAGE__->deflate_timepiece(@_) } );
__PACKAGE__->has_a( due => 'Time::Piece',
		    inflate => sub { return __PACKAGE__->inflate_timepiece(@_,'due'); },
		    deflate => sub { return __PACKAGE__->deflate_timepiece(@_) } );

__PACKAGE__->has_a( started => 'Time::Piece',
		    inflate => sub { return __PACKAGE__->inflate_timepiece(@_,'started'); },
		    deflate => sub { return __PACKAGE__->deflate_timepiece(@_) } );
__PACKAGE__->has_a( finished => 'Time::Piece',
		    inflate => sub { return __PACKAGE__->inflate_timepiece(@_,'finished'); },
		    deflate => sub { return __PACKAGE__->deflate_timepiece(@_) } );

__PACKAGE__->set_sql (errors => <<END_ERRORS_SQL, 0);
  SELECT * FROM task_history
  where error is not null
  and status = 'error'
  order by deadline desc
  limit 100
END_ERRORS_SQL

__PACKAGE__->set_sql (warnings => <<END_WARNINGS_SQL, 0 );
  SELECT th.* FROM task_history th
  where warning like '%warning%'
  order by deadline desc
  limit 100
END_WARNINGS_SQL

# called when task fails, succedes or times out
sub create_from_task {
  my ($class,$task) = @_;
  my $self = $class->create({
			     task => $task->id,
			     task_name => $task->name,
			     command => $task->command->name,
			     command_arguments => $task->command_arguments,
			     status => $task->last_status,
			     priority => $task->priority,
			     error => $task->error,
			     started => $task->last_run,
			     finished => $task->last_completed,
			     deadline => $task->last_deadline,
			     due => $task->last_due,
			     node => $task->last_node,
			     warning => $task->warning,
			    });
  $self->db_Main->{CachedKids} = {};
  return $self->id;
}

1;


__END__


=head2 METHODS PROVIDED ELSEWHERE

Several additional methods for the Monitor Web UI are provided in
Schedule::Chronic::Distributed::Monitor::TaskHistory package.

These are :

=over

=item errors

=item warnings

=back

=cut


=head1 SEE ALSO

Schedule::Chronic::Distributed::Task

Schedule::Chronic::Distributed::Monitor::TaskHistory

=head1 AUTHOR

aaron trevena, E<lt>aaron.trevena@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Aaron Trevena, Aviation Briefing Ltd

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.

=cut
